package com.carkeeper.employee.service.impl;

import com.carkeeper.department.dao.DepartmentMapper;
import com.carkeeper.employee.dao.EmployeeMapper;
import com.carkeeper.employee.service.EmployeeService;
import com.carkeeper.pojo.Department;
import com.carkeeper.pojo.Employee;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private DepartmentMapper departmentMapper;


    @Override
    public List<Employee> findAll() {

        List<Employee> list = employeeMapper.selectAll();
        return list;
    }

    @Override
    public void add(Employee employee) {

        employeeMapper.insert(employee);
    }

    @Override
    public Page<Employee> findPage(Map searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<Employee>)employeeMapper.selectByExample(example);
    }

    @Override
    public List showList() {
       List list = departmentMapper.showList();
        return list;
    }

    @Override
    public void update(Employee employee) {

        employeeMapper.updateByPrimaryKey(employee);
    }

    /**
     * 锁定用户
     * @param id
     */
    @Override
    public Employee lockBlock(Integer id) {

        Employee employee = employeeMapper.selectByPrimaryKey(id);

        if (employee == null){

            throw new RuntimeException("id不存在");
        }
        if (employee.getAccount_status() != 1){

            employee.setAccount_status(1);


        }

        return employee;

    }

    @Override
    public Employee findById(Integer id) {

        Employee employeeList = employeeMapper.selectByPrimaryKey(id);


        return  employeeList;

    }


    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Department.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 分类名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // 是否显示
            if (searchMap.get("isShow") != null && !"".equals(searchMap.get("isShow"))) {
                criteria.andEqualTo("isShow", searchMap.get("isShow"));
            }
            // 是否导航
            if (searchMap.get("isMenu") != null && !"".equals(searchMap.get("isMenu"))) {
                criteria.andLike("isMenu", "%" + searchMap.get("isMenu") + "%");
            }

            // 分类ID
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            // 商品数量
            if (searchMap.get("goodsNum") != null) {
                criteria.andEqualTo("goodsNum", searchMap.get("goodsNum"));
            }
            // 排序
            if (searchMap.get("seq") != null) {
                criteria.andEqualTo("seq", searchMap.get("seq"));
            }
            // 上级ID
            if (searchMap.get("parentId") != null) {
                criteria.andEqualTo("parentId", searchMap.get("parentId"));
            }
            // 模板ID
            if (searchMap.get("templateId") != null) {
                criteria.andEqualTo("templateId", searchMap.get("templateId"));
            }

        }
        return example;
    }
}
